#import "Preferences.h"

#import "PreferencesData.h"
#import "Utility.h"

@implementation Preferences

//------------------------------------------------------------------------------
- (id) initWithPrefs:(PreferencesData*) preferencesData
{
	self = [super init];
	if (nil != self)
	{
		prefData = preferencesData;
		[prefData retain];
	}

	return self;
}

//------------------------------------------------------------------------------
- (void) awakeFromNib
{
	[gConfirmTrash setState: [prefData confirmMoveToTrash]];
	[gUseAppleRemote setState: [prefData useAppleRemote]];
	[gStartupWindow selectCellAtRow: [prefData startupWindowType]  column: 0];

	tempStartupFolder = [prefData startupFolder];
	if (nil != tempStartupFolder)
	{
		[tempStartupFolder retain];
	}

	[gStartupFolder selectCellAtRow: [prefData startupFolderType]  column: 0];
	if (STARTUP_FOLDER_SPECIFIC == [prefData startupFolderType])
	{
		[self gnrlEnableFolderSelection:nil];
	}


	[bRegenerateMisMatched setState: [prefData regenerateMismatchedThumbs]];
	[bMarkGeneratedThumbnails setState: [prefData markGeneratedThumbnails]];
	[bShowHiddenFolders setState: [prefData showHiddenFolders]];
	BOOL customColor = nil != [prefData browserBackgroundColor];
	[bBackColorMatrix selectCellAtRow: nil != customColor  column: 0];
	if (YES == customColor)
	{
		[self brwsrEnableBackColorWell:nil];
		[bBackColorButton setColor: [prefData browserBackgroundColor]];
	}
	customColor = nil != [prefData browserTextColor];
	[bTextColorMatrix selectCellAtRow: nil != customColor  column: 0];
	if (YES == customColor)
	{
		[self brwsrEnableTextColorWell:nil];
		[bTextColorButton setColor: [prefData browserTextColor]];
	}


	[vUseSingleWindow setState: [prefData useSingleWindow]];
	[vUseNaturalMovie setState: [prefData useMovieNaturalSize]];
	[vPageJump setIntValue: [prefData pageJumpSize]];
	[vPathElements setIntValue: [prefData pathElements]];
	customColor = nil != [prefData viewerBackgroundColor];
	[vBackColorMatrix selectCellAtRow: nil != customColor  column: 0];
	if (YES == customColor)
	{
		[self vwrEnableBackColorWell:nil];
		[vBackColorButton setColor: [prefData viewerBackgroundColor]];
	}
	customColor = nil != [prefData viewerTextColor];
	[vTextColorMatrix selectCellAtRow: nil != customColor  column: 0];
	if (YES == customColor)
	{
		[self vwrEnableTextColorWell:nil];
		[vTextColorButton setColor: [prefData viewerTextColor]];
	}


	[sDelay setIntValue: [prefData slideshowDelay]];
	[sOrder selectCellAtRow: [prefData slideshowOrder]  column: 0];
	customColor = nil != [prefData slideshowBackgroundColor];
	[sBackColorMatrix selectCellAtRow: nil != customColor  column: 0];
	if (YES == customColor)
	{
		[self slideEnableBackColorWell: nil];
		[sBackColorButton setColor: [prefData slideshowBackgroundColor]];
	}
	customColor = nil != [prefData slideshowTextColor];
	[sTextColorMatrix selectCellAtRow: nil != customColor  column: 0];
	if (YES == customColor)
	{
		[self slideEnableTextColorWell: nil];
		[sTextColorButton setColor: [prefData slideshowTextColor]];
	}
	[sFolder setStringValue: [prefData slideshowFolder]];
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
- (IBAction) cancelClicked:(id) sender
{
	[[NSApplication sharedApplication] abortModal];
	[mainWindow close];
	[mainWindow release];
}

//------------------------------------------------------------------------------
- (IBAction) okClicked:(id) sender
{
	[prefData setConfirmMoveToTrash: [gConfirmTrash state]];
	[prefData setUseAppleRemote: [gUseAppleRemote state]];
	[prefData setStartupWindowType: [gStartupWindow selectedRow]];
	[prefData setStartupFolderType: [gStartupFolder selectedRow]];

	if (STARTUP_FOLDER_SPECIFIC == [prefData startupFolderType])
	{
		[prefData setStartupFolder: [gStartupFolderText stringValue]];
	}


	[prefData setRegenerateMismatchedThumbs: [bRegenerateMisMatched state]];
	[prefData setMarkGeneratedThumbnails: [bMarkGeneratedThumbnails state]];
	[prefData setShowHiddenFolders: [bShowHiddenFolders state]];
	if (0 != [bBackColorMatrix selectedRow])
	{
		[prefData setBrowserBackgroundColor: [bBackColorButton color]];
	}
	else
	{
		[prefData setBrowserBackgroundColor: nil];
	}
	if (0 != [bTextColorMatrix selectedRow])
	{
		[prefData setBrowserTextColor: [bTextColorButton color]];
	}
	else
	{
		[prefData setBrowserTextColor: nil];
	}


	[prefData setPageJumpSize: [vPageJump intValue]];
	[prefData setPathElements: [vPathElements intValue]];
	[prefData setUseSingleWindow: [vUseSingleWindow state]];
	[prefData setUseMovieNaturalSize: [vUseNaturalMovie state]];
	if (0 != [vBackColorMatrix selectedRow])
	{
		[prefData setViewerBackgroundColor: [vBackColorButton color]];
	}
	else
	{
		[prefData setViewerBackgroundColor: nil];
	}
	if (0 != [vTextColorMatrix selectedRow])
	{
		[prefData setViewerTextColor: [vTextColorButton color]];
	}
	else
	{
		[prefData setViewerTextColor: nil];
	}


	// Slideshow
	[prefData setSlideshowDelay: [sDelay intValue]];
	[prefData setSlideshowOrder: [sOrder selectedRow]];
	if (0 != [sBackColorMatrix selectedRow])
	{
		[prefData setSlideshowBackgroundColor: [sBackColorButton color]];
	}
	else
	{
		[prefData setSlideshowBackgroundColor: nil];
	}
	if (0 != [sTextColorMatrix selectedRow])
	{
		[prefData setSlideshowTextColor: [sTextColorButton color]];
	}
	else
	{
		[prefData setSlideshowTextColor: nil];
	}
	[prefData setSlideshowFolder: [sFolder stringValue]];


	[[NSApplication sharedApplication] stopModal];
	[mainWindow close];
	[mainWindow release];

	NSNotification *note = [NSNotification notificationWithName
		: PBPrefencesDidChangeNotification
		object: prefData];

	[[NSNotificationCenter defaultCenter] postNotification: note];
}

//------------------------------------------------------------------------------
- (IBAction) chooseFolder:(id) sender
{
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	[openPanel setCanChooseFiles:NO];
	[openPanel setCanChooseDirectories:YES];

	NSString *dir = GetPicturesFolder();

	int retCode = [openPanel runModalForDirectory
		: dir
		file:nil 
		types:nil];

	if (NSOKButton == retCode)
	{
		[gStartupFolderText setStringValue: [openPanel filename]];
	}
}

//------------------------------------------------------------------------------
- (IBAction) slideChooseFolder:(id) sender
{
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	[openPanel setCanChooseFiles:NO];
	[openPanel setCanChooseDirectories:YES];

	NSString *dir = GetPicturesFolder();

	int retCode = [openPanel runModalForDirectory
		: dir
		file:nil 
		types:nil];

	if (NSOKButton == retCode)
	{
		[sFolder setStringValue: [openPanel filename]];
	}
}


//------------------------------------------------------------------------------
- (IBAction) brwsrDisableBackColorWell:(id) sender
{
	[bBackColorButton setEnabled: NO];
}

//------------------------------------------------------------------------------
- (IBAction) brwsrEnableBackColorWell:(id) sender
{
	[bBackColorButton setEnabled: YES];
}

//------------------------------------------------------------------------------
- (IBAction) brwsrDisableTextColorWell:(id) sender
{
	[bTextColorButton setEnabled: NO];
}

//------------------------------------------------------------------------------
- (IBAction) brwsrEnableTextColorWell:(id) sender
{
	[bTextColorButton setEnabled: YES];
}


//------------------------------------------------------------------------------
- (IBAction) gnrlDisableFolderSelection:(id) sender
{
	// Save the startup folder so it's available if they go back
	[tempStartupFolder release];
	tempStartupFolder = [gStartupFolderText stringValue];
	[tempStartupFolder retain];
	[gStartupFolderText setTitleWithMnemonic:@""];

	[gStartupFolderChoose setEnabled: NO];
}

//------------------------------------------------------------------------------
- (IBAction) gnrlEnableFolderSelection:(id) sender
{
	[gStartupFolderChoose setEnabled: YES];
	[gStartupFolderText setTitleWithMnemonic: tempStartupFolder];
}


//------------------------------------------------------------------------------
- (IBAction) slideDisableBackColorWell:(id) sender
{
	[sBackColorButton setEnabled: NO];
}

//------------------------------------------------------------------------------
- (IBAction) slideDisableTextColorWell:(id) sender;
{
	[sTextColorButton setEnabled: NO];
}

//------------------------------------------------------------------------------
- (IBAction) slideEnableBackColorWell:(id) sender
{
	[sBackColorButton setEnabled: YES];
}

//------------------------------------------------------------------------------
- (IBAction) slideEnableTextColorWell:(id) sender
{
	[sTextColorButton setEnabled: YES];
}


//------------------------------------------------------------------------------
- (IBAction) vwrDisableBackColorWell:(id) sender
{
	[vBackColorButton setEnabled: NO];
}

//------------------------------------------------------------------------------
- (IBAction) vwrEnableBackColorWell:(id) sender
{
	[vBackColorButton setEnabled: YES];
}

//------------------------------------------------------------------------------
- (IBAction) vwrDisableTextColorWell:(id) sender
{
	[vTextColorButton setEnabled: NO];
}

//------------------------------------------------------------------------------
- (IBAction) vwrEnableTextColorWell:(id) sender
{
	[vTextColorButton setEnabled: YES];
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
- (NSWindow*) mainWindow
{
	return mainWindow;
}

@end
